package cn.smile.util;

import java.lang.reflect.Field;

import cn.smile.annotion.ValidateEntity;
import cn.smile.bean.CodeMsg;
import lombok.extern.slf4j.Slf4j;

/**
 * 验证实体工具类
 */
@Slf4j
public class ValidateEntityUtil {

    public static CodeMsg validate(Object object) {
        Field[] declaredFields = object.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            ValidateEntity annotation = field.getAnnotation(ValidateEntity.class);
            if (annotation != null) {
                if (annotation.required()) {
                    //表示该字段是必填字段
                    field.setAccessible(true);
                    try {
                        Object o = field.get(object);
                        //首先判断是否为空
                        if (o == null) {
                            CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                            codeMsg.setMsg(annotation.errorRequiredMsg());
                            return codeMsg;
                        }
                        //到这，说明该变量的值不是null
                        //首先判断是不是String类型
                        if (o instanceof String) {
                            //若是字符串类型，则检查其长度
                            if (annotation.requiredLength()) {
                                if (o.toString().length() < annotation.minLength()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMinLengthMsg());
                                    return codeMsg;
                                }
                                if (o.toString().length() > annotation.maxLength()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMaxLengthMsg());
                                    return codeMsg;
                                }
                            }
                        }
                        //其次来判断是否为数字
                        if (isNumberObject(o)) {
                            //判断是否规定检查最小值
                            if (annotation.requiredMinValue()) {
                                if (Double.parseDouble(o.toString()) < annotation.minValue()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMinValueMsg());
                                    return codeMsg;
                                }
                            }
                            //判断是否规定检查最大值
                            if (annotation.requiredMaxValue()) {
                                if (Double.parseDouble(o.toString()) > annotation.maxValue()) {
                                    CodeMsg codeMsg = CodeMsg.VALIDATE_ENTITY_ERROR;
                                    codeMsg.setMsg(annotation.errorMaxValueMsg());
                                    return codeMsg;
                                }
                            }
                        }
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        log.error("处理异常, e: ", e);
                    }
                }
            }
        }
        return CodeMsg.SUCCESS;
    }

    /**
     * 检查对象是否是数字类型
     */
    public static boolean isNumberObject(Object object) {
        if (object instanceof Integer) return true;
        if (object instanceof Long) return true;
        if (object instanceof Float) return true;
        return object instanceof Double;
    }
}
